package com.kpx.app.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.OrderBean;
import com.kpx.app.bean.OrderDetailBean;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.DialogFactoryImpl;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by congdy on 17/6/25.
 */

public class ActivityBuyMyself extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.img_task)
    ImageView imgTask;
    @BindView(R.id.txt_pro_name)
    TextView txtProName;
    @BindView(R.id.txt_sex)
    TextView txtSex;
    @BindView(R.id.txt_age)
    TextView txtAge;
    @BindView(R.id.txt_height)
    TextView txtHeight;
    @BindView(R.id.txt_weight)
    TextView txtWeight;
    @BindView(R.id.txt_return)
    TextView txtReturn;
    @BindView(R.id.txt_danbao_price)
    TextView txtDanbaoPrice;
    @BindView(R.id.txt_yj)
    TextView txtYj;
    @BindView(R.id.txt_yongjin_price)
    TextView txtYongjinPrice;
    @BindView(R.id.btn_suborder)
    Button btnSuborder;
    @BindView(R.id.txt_price)
    TextView txtPrice;
    OrderDetailBean detailbean;
    OrderBean b;
    @BindView(R.id.txt_star)
    TextView txtStar;
    @BindView(R.id.txt_shose_size)
    TextView txtShoseSize;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_buy_myself);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "我要自购");
    }

    @Override
    public void initData() {
        b = (OrderBean) getIntent().getExtras().getSerializable(Constants.KEY_ORDER_BEAN);
        if (b != null) {
            getOrderDetail(b.getDmId());
        }
    }

    DecimalFormat df = new DecimalFormat("###0.00");

    private String getSex(OrderDetailBean.RequirementMapBean sex) {
        if (sex == null) {
            return "不限";
        }
        switch (sex.getSex()) {
            case 0:
                return "不限";
            case 1:
                return "男";
            case 2:
                return "女";
            default:
                return "不限";
        }
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }


    @OnClick(R.id.btn_suborder)
    public void onViewClicked() {
        DialogFactoryImpl.newInstance(this).showConfirmDialog("取消", "确定", "确定自购商品吗", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                switch (which) {
                    case DialogInterface.BUTTON_NEGATIVE:
                        break;
                    case DialogInterface.BUTTON_POSITIVE:
                        toDoOrder();
                        break;
                }
            }
        });
    }

    public void toDoOrder() {
        showLoadingDialog();
        final MyHashMap map = new MyHashMap();
        map.put("orderId", b.getDmId() + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    ToastUtil.getInstance().showToast(ActivityBuyMyself.this, "自购成功");
                    sendBroadcast(new Intent(BroadcastFilters.BROADCAST_ACTION_REFRESH_DATA));
                } else {
                    ToastUtil.getInstance().showToast(ActivityBuyMyself.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityBuyMyself.this, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_TO_BUY_GOODS, true, map);

    }

    /**
     * 获取订单详情
     */
    private void getOrderDetail(long dmid) {
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("orderId", dmid + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    detailbean = (OrderDetailBean) result.getData();
                    if (detailbean != null) {
                        setData();
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityBuyMyself.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityBuyMyself.this, error);
            }

        }, OrderDetailBean.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_ORDER_DETAIL, true, map);
    }

    private void setData() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.ic_launcher)  //设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中
                .imageScaleType(ImageScaleType.EXACTLY)//设置图片以如何的编码方式显示
                .build();//不推荐用！！！！是否设置为圆角，弧度为多少
        if (b != null) {
            ImageLoader.getInstance().displayImage(TextUtils.isEmpty(b.getTaskGoods().getMainPic()) ? "--" : b.getTaskGoods().getMainPic(), imgTask, options);
            txtProName.setText(b.getTaskGoods().getGoodsName());
        }
        txtSex.setText("性别：" + getSex(detailbean.getRequirementMap()));
        txtAge.setText("年龄：" + (detailbean.getRequirementMap() == null || UIUtils.isEmptyTask(detailbean.getRequirementMap().getAge()) ? "不限" : (detailbean.getRequirementMap().getAge() + "")));
        txtHeight.setText("身高：" + (detailbean.getRequirementMap() == null || UIUtils.isEmptyTask(detailbean.getRequirementMap().getHeight()) ? "不限" : detailbean.getRequirementMap().getHeight() + ""));
        txtWeight.setText("体重：" + (detailbean.getRequirementMap() == null || UIUtils.isEmptyTask(detailbean.getRequirementMap().getWeight()) ? "不限" : detailbean.getRequirementMap().getWeight() + ""));
        txtStar.setText("等级：" + (detailbean.getRequirementMap() == null || UIUtils.isEmptyTask(detailbean.getRequirementMap().getLevel()) ? "不限" : detailbean.getRequirementMap().getLevel() + ""));
        txtShoseSize.setText("鞋码：" + (detailbean.getRequirementMap() == null || UIUtils.isEmptyTask(detailbean.getRequirementMap().getSize()) ? "不限" : detailbean.getRequirementMap().getSize() + ""));
        txtReturn.setText("＊佣金包含退货运费   退货地址：" + (UIUtils.isEmpty(detailbean.getRefundAddress()) ? "--" : detailbean.getRefundAddress()));
        txtDanbaoPrice.setText(df.format(b.getDeposit()));
        txtReturn.setVisibility(detailbean.getOrderType() == 1 ? View.VISIBLE : View.GONE);
        txtYongjinPrice.setText(b.getOrderType() == 1 ? df.format(detailbean.getCommission()) : "赠送实物");
        findViewById(R.id.txt_yj).setVisibility(b.getOrderType() == 1 ? View.VISIBLE : View.GONE);
        txtPrice.setText("¥" + df.format(b.getPurchaseAmount()));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
